package com.sqrj.phoneorder.service;


import com.sqrj.phoneorder.Vo.WeighNameVo;
import com.sqrj.phoneorder.Vo.WeighProUnitVo;
import com.sqrj.phoneorder.Vo.WeighStorageVo;
import com.sqrj.phoneorder.Vo.WeighTruckVo;
import com.sqrj.phoneorder.dao.GybWeighRecordMapper;
import com.sqrj.phoneorder.dto.WeighCountDto;
import com.sqrj.phoneorder.model.GybWeighRecord;
import com.sqrj.phoneorder.param.WeighCountParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SysWeighCountService {
    @Autowired
    private GybWeighRecordMapper gybWeighRecordMapper;

    //按客户
    public List<WeighProUnitVo> getWeighCountByProUnit(WeighCountParam weighCountParam) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        List<WeighProUnitVo> weighProUnitVoList = gybWeighRecordMapper.getPageListByProUnit(weighCountDto);

        return weighProUnitVoList;
    }

    //查询客户集合
    public List<String> queryWeightCustomerList(WeighCountParam weighCountParam) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        List<String> customerList = gybWeighRecordMapper.getWeightCustomerList(weighCountDto);

        customerList.add(0,"全部");

        return  customerList;
    }


    //按车牌号
    public List<WeighTruckVo> getWeighCountByTruck(WeighCountParam weighCountParam) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        List<WeighTruckVo> weighTruckVoList = gybWeighRecordMapper.getPageListByTruck(weighCountDto);

        return weighTruckVoList;
    }

    //查询车牌号集合
    public List<String> queryWeightTruckList(WeighCountParam weighCountParam) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        List<String> truckList = gybWeighRecordMapper.getWeightTruckList(weighCountDto);

        truckList.add(0,"全部");

        return  truckList;
    }

    public List<WeighStorageVo> getWeighCountByStorage(WeighCountParam weighCountParam) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        List<WeighStorageVo> weighStorageVoList = gybWeighRecordMapper.getPageListByStorage(weighCountDto);

        return weighStorageVoList;
    }

    //查询货物名称集合
    public List<String> queryWeightStorageList(WeighCountParam weighCountParam) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        List<String> typeList = gybWeighRecordMapper.getWeightStorageList(weighCountDto);

        typeList.add(0,"全部");

        return  typeList;
    }

    public List<WeighNameVo> getWeighCountByName(WeighCountParam weighCountParam) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        List<WeighNameVo> weighNameVoList = gybWeighRecordMapper.getPageListByName(weighCountDto);

        return weighNameVoList;

    }

    //查询货物名称集合
    public List<String> queryWeightTypeList(WeighCountParam weighCountParam) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        List<String> typeList = gybWeighRecordMapper.getWeightTypeList(weighCountDto);

        typeList.add(0,"全部");

        return  typeList;
    }

//  封装查询数据，时间赋默认值
    private static WeighCountDto getWeighCDto(WeighCountParam weighCountParam){
        WeighCountDto weighCountDto = new WeighCountDto();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (StringUtils.isNotBlank(weighCountParam.getFromTime())) {
                weighCountDto.setFromTime(dateFormat.parse(weighCountParam.getFromTime()+" 00:00:00"));
            }else{
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, 0);
                // 分
                cal.set(Calendar.MINUTE, 0);
                // 秒
                cal.set(Calendar.SECOND, 0);
                // 毫秒
                cal.set(Calendar.MILLISECOND, 0);
//                cal.add(Calendar.DATE, -7);
                weighCountDto.setFromTime(cal.getTime());
            }
            if (StringUtils.isNotBlank(weighCountParam.getToTime())) {
                weighCountDto.setToTime(dateFormat.parse(weighCountParam.getToTime()+" 23:59:59"));
            }else{
                Calendar calTo = Calendar.getInstance();
                calTo.set(Calendar.HOUR_OF_DAY, 23);
                // 分
                calTo.set(Calendar.MINUTE, 59);
                // 秒
                calTo.set(Calendar.SECOND, 59);
                // 毫秒
                calTo.set(Calendar.MILLISECOND, 0);
                weighCountDto.setToTime(calTo.getTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        weighCountDto.setJbzCode(weighCountParam.getJbzCode());


        if(weighCountParam.getSpecialparam() == null || weighCountParam.getSpecialparam().equals("全部")){
            System.out.println("------为空");
        }else {
            weighCountDto.setSpecialparam(weighCountParam.getSpecialparam());
        }

        return weighCountDto;
    }
//  查询过磅记录集合
    public List<GybWeighRecord> queryWeightRecord(WeighCountParam weighCountParam) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        return gybWeighRecordMapper.queryWeightRecord(weighCountDto);

    }


//    public List<WeighTruckVo> getWeighCountByTruck(WeighCountParam weighCountParam) {
//
//        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);
//
//        int count = gybWeighRecordMapper.getWeighCountByTruck(weighCountDto);
//
//        if (count > 0){
//            List<WeighTruckVo> weighTruckVoList = gybWeighRecordMapper.getPageListByTruck(weighCountDto, page);
//            return PageResult.<WeighTruckVo>builder().total(count).data(weighTruckVoList).build();
//        }
//        return PageResult.<WeighTruckVo>builder().build();
//    }
//
//    public List<WeighStorageVo> getWeighCountByStorage(WeighCountParam weighCountParam) {
//
//        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);
//
//        int count = gybWeighRecordMapper.getWeighCountByStorage(weighCountDto);
//
//        if (count > 0){
//            List<WeighStorageVo> weighStorageVoList = gybWeighRecordMapper.getPageListByStorage(weighCountDto, page);
//            return PageResult.<WeighStorageVo>builder().total(count).data(weighStorageVoList).build();
//        }
//        return PageResult.<WeighStorageVo>builder().build();
//    }
//
//    public List<WeighNameVo> getWeighCountByName(WeighCountParam weighCountParam) {
//
//        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);
//
//        int count = gybWeighRecordMapper.getWeighCountByName(weighCountDto);
//
//        if (count > 0){
//            List<WeighNameVo> weighNameVoList = gybWeighRecordMapper.getPageListByName(weighCountDto, page);
//            return PageResult.<WeighNameVo>builder().total(count).data(weighNameVoList).build();
//        }
//        return PageResult.<WeighNameVo>builder().build();
//
//    }
}
